﻿Imports oAuth.OAuth
Imports System.Web
Imports System.Text
Imports oAuth.OAuth.OAuthBase

Public Class Form1
    'Const Key = "aMkjUB4uzpYKuQa5gqOcOw"
    'Const Secret = "gfHFcjKNQhWpTBUu5jr8LokOpl9K0KFB4XENzGmDc"
    'Const Request_Token_Address = "http://api.soundcloud.com/oauth/request_token"
    'Const Access_Token_Address = "http://api.soundcloud.com/oauth/access_token"
    'Const Authorize_Address = "http://api.soundcloud.com/oauth/authorize"

    Const Key = "key"
    Const Secret = "secret"
    Const Request_Token_Address = "http://term.ie/oauth/example/request_token.php"
    Const Access_Token_Address = "http://term.ie/oauth/example/access_token.php"
    Const Authorize_Address = "http://term.ie/oauth/example/echo_api.php?name=andrew"



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ToAuth As New OAuth.OAuthBase()
        Dim nonce As String = ToAuth.GenerateNonce()
        Dim timeStamp As String = ToAuth.GenerateTimeStamp()
        Dim surl As String = ""
        Dim sParms As String = ""
        Dim RequestResult As String = ""
        Dim RequestParameters As Generic.List(Of QueryParameter)

        '======
        'Step 1
        '======
        Dim sig As String = ToAuth.GenerateSignature(New System.Uri(Request_Token_Address), Key, Secret, String.Empty, String.Empty, "GET", _
        timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, surl, sParms)
        sig = HttpUtility.UrlEncode(sig)
        RequestResult = HttpRequest.Send(surl & "?" & sParms & "&oauth_signature=" & sig)

       
        '======
        'Step 2
        '======
        RequestParameters = ToAuth.GetResultParameters(RequestResult)

        Dim Parameter = From QueryParameter In RequestParameters Where QueryParameter.Name = "oauth_token"
        Dim oauth_token As String = Parameter(0).Value

        Parameter = From QueryParameter In RequestParameters Where QueryParameter.Name = "oauth_token_secret"
        Dim oauth_token_secret As String = Parameter(0).Value

        sig = ToAuth.GenerateSignature(New Uri(Access_Token_Address), Key, Secret, oauth_token, oauth_token_secret, "GET", _
        timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, surl, sParms)
        sig = HttpUtility.UrlEncode(sig)
        Debug.Print(surl & "?" & sParms & "&oauth_signature=" & sig)
        RequestResult = HttpRequest.Send(surl & "?" & sParms & "&oauth_signature=" & sig)
        
        '======
        'Step 3
        '======
        RequestParameters = ToAuth.GetResultParameters(RequestResult)

        Parameter = From QueryParameter In RequestParameters Where QueryParameter.Name = "oauth_token"
        oauth_token = Parameter(0).Value

        Parameter = From QueryParameter In RequestParameters Where QueryParameter.Name = "oauth_token_secret"
        oauth_token_secret = Parameter(0).Value
        
        sig = ToAuth.GenerateSignature(New Uri(Authorize_Address), Key, Secret, oauth_token, oauth_token_secret, "GET", _
        timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, surl, sParms)
        sig = HttpUtility.UrlEncode(sig)
        RequestResult = HttpRequest.Send(surl & "?" & sParms & "&oauth_signature=" & sig)
        
        Debug.Print(RequestResult)
       
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        AxWindowsMediaPlayer1.URL = "http://media.soundcloud.com/stream/jhUB4qL7anDu"
        AxWindowsMediaPlayer1.Ctlcontrols.play()
    End Sub
End Class
